package com.edu.controller;

import com.edu.pojo.Announcement;
import com.edu.service.AnnouncementService;
import com.edu.service.FileService;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping(value="/fileController")
public class FileController {

    @Autowired
    private FileService fileService;

    @Autowired
    private AnnouncementService announcementService;

    @RequestMapping(value="/showFile")
    public String showFile(Model model) {
        List<com.edu.pojo.File> list = fileService.showAllFile();
        SimpleDateFormat time=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        for(int i=0;i<list.size();i++) {
            Date time4db = list.get(i).getInputTime();
            String formatTime = time.format(time4db);
            list.get(i).setFormatTime(formatTime);
        }
        model.addAttribute("FileList", list);
        return "common/File";
    }

    @RequestMapping(value="/showNeedFile")
    public String showNeedFile(Model model,int state) {
        List<com.edu.pojo.File> list = fileService.showNeedFile(state);
        SimpleDateFormat time=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        for(int i=0;i<list.size();i++) {
            Date time4db = list.get(i).getInputTime();
            String formatTime = time.format(time4db);
            list.get(i).setFormatTime(formatTime);
        }
        model.addAttribute("FileList", list);
        return "common/File";
    }

    @RequestMapping(value="/showannouncement")
    public String Announcement(Model model) {
        List<Announcement> list = announcementService.queryAnnouncement();
        SimpleDateFormat time=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        for(int i=0;i<list.size();i++) {
            Date time4db = list.get(i).getInputTime();
            String formatTime = time.format(time4db);
            list.get(i).setFormatTime(formatTime);
        }
        model.addAttribute("announcementList", list);
        return "common/Announcement";
    }



    @RequestMapping(value="/fileDownload")
    public ResponseEntity<byte[]> fileDownload(HttpServletRequest request, @RequestParam("filePath") String filePath, @RequestParam("fileName") String fileName, Model model) throws Exception {
        File file = new File(filePath);
        HttpHeaders headers = new HttpHeaders();
        String downloadFile = new String(fileName.getBytes("utf-8"), "iso-8859-1");
        //以浏览器形式打开
        headers.setContentDispositionFormData("attachment", downloadFile);
        //以流的形式下载文件
        headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
        //以spring mvc 中的Re封装对象，返回下载内容
        return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file),headers, HttpStatus.CREATED);
    }
}
